package com.hsj.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hsj.demo.entity.Group;
import com.hsj.demo.entity.User;
import com.hsj.demo.entity.UserFriend;
import com.hsj.demo.service.UserFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/userFriend")
public class UserFriendController {
    @Autowired
    private UserFriendService userFriendService;

    @RequestMapping("/getRecentlyFriend")
    public List<UserFriend> getRecentlyFriend(Integer id){
        return userFriendService.findBySourceId(id,'1');
    }

    @RequestMapping("/getAllFriend")
    public List<UserFriend> getAllFriend(HttpSession session){
        User user = (User)session.getAttribute("user");
        return userFriendService.findBySourceId(user.getId(),null);
    }

    @RequestMapping("/agreeInsert")
    public String agreeInsert(UserFriend userFriend){
        userFriendService.agreeInsert(userFriend);
        return "成功";
    }

    @RequestMapping("/notAgreeInsert")
    public String notAgreeInsert(UserFriend userFriend){
        userFriendService.deleteByEntity(userFriend);
        return "成功";
    }

    @RequestMapping("/updateGroup")
    public String updateGroup(HttpSession session, UserFriend userFriend){
        User user = (User)session.getAttribute("user");
        return userFriendService.updateGroup(userFriend, user);
    }

    @RequestMapping("/findByKeyword")
    public List<UserFriend> findByKeyword(@RequestBody UserFriend userFriend,HttpSession session){
        User user = (User)session.getAttribute("user");
        return userFriendService.findByKeyword(userFriend,user);
    }

    @RequestMapping("/updateRecently")
    public String updateRecently(HttpSession session,UserFriend userFriend){
        User user = (User)session.getAttribute("user");
        userFriend.setSourceUserId(user.getId());
        userFriendService.update(userFriend);
        return "OK";
    }

    @RequestMapping("/findChatHistory")
    public List<UserFriend> findChatHistory(HttpSession session){
        User user = (User)session.getAttribute("user");
        return userFriendService.findChatHistory(user.getId());
    }
}
